package cn.zz.cameralibrary;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.zz.cameralibrary.model.Constants;
import cn.zz.cameralibrary.utils.ImageUtils;
import cn.zz.cameralibrary.view.holocolorpicker.SelectColorPopup;
import cn.zz.cameralibrary.view.sticker.OperateConstants;
import cn.zz.cameralibrary.view.textstickerview.TextStickerView;

import static cn.zz.cameralibrary.R.id.mainLayout;

/**
 * author: zhu on 2017/7/13 18:00
 * email: mackkilled@gmail.com
 */

public class AddTextActivity extends BaseActivity implements View.OnClickListener {
    private Bitmap sourceMap;
    private Button text_color, text_typeface;
    private EditText text_input;
    private TextView btn_done;
    private String typeface;
    private SelectColorPopup menuWindow;

    private LinearLayout content_layout;

    private TextStickerView stickerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camerasdk_activity_addtext);
        showLeftIcon();
        setActionBarTitle("文字添加");
        findviews();
        setListener();
    }

    private void findviews() {

        content_layout = (LinearLayout) findViewById(mainLayout);
        stickerView = (TextStickerView) findViewById(R.id.textStickerView);

        text_input = (EditText) findViewById(R.id.text_input);
        text_color = (Button) findViewById(R.id.text_color);
        text_typeface = (Button) findViewById(R.id.text_typeface);
        text_typeface = (Button) findViewById(R.id.text_typeface);

        btn_done = (TextView) findViewById(R.id.camerasdk_title_txv_right_text);
        btn_done.setVisibility(View.VISIBLE);
        btn_done.setText("确定");

        menuWindow = new SelectColorPopup(AddTextActivity.this);

        content_layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                content_layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                sourceMap = ImageUtils.compressionFiller(AddTextActivity.this, Constants.bitmap, content_layout);
                addText();
            }
        });
    }

    public void addText() {
        stickerView.setBackgroundBitmap(sourceMap);

        stickerView.setEditText(text_input);
    }

    private void setListener() {
        text_color.setOnClickListener(this);
        text_typeface.setOnClickListener(this);
        btn_done.setOnClickListener(this);
        menuWindow.setOnClickDismisser(new SelectColorPopup.onClickDismisser() {
            @Override
            public void onDissmiss(int color) {
                stickerView.setTextColor(color);
                Log.i("TAG", "onClick: dismisscolor:" + color);
            }
        });

        text_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                stickerView.setText(s.toString());
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.text_color) {
            menuWindow.showAtLocation(AddTextActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            return;
        }
        if (id == R.id.text_typeface) {
            typeface = OperateConstants.FACE_BY;
            stickerView.setTypefaceObj(this, typeface);
            return;
        }
        if (id == R.id.camerasdk_title_txv_right_text) {
            Constants.bitmap = stickerView.getDrawBitmap();
            setResult(Constants.RequestCode_Croper);
            finish();
            return;
        }
    }

}
